powerhua.cms = {};

// 参数 
var d; // 列表数据

var ds = [
	{width:"20px", borderBottom:"solid 1px #eee", padding:"3px 4px 3px 4px"}, 
	{width:"20px", borderBottom:"solid 1px #eee", padding:"2px 4px 2px 5px"},
	{borderBottom:"solid 1px #eee", padding:"2px 4px 2px 5px", fontWeight:"bolder"}, 
	{width:"200px", borderBottom:"solid 1px #eee", padding:"2px 4px 2px 5px"}, 
	{width:"100px", borderBottom:"solid 1px #eee", padding:"2px 4px 2px 5px"},  
	{width:"120px", borderBottom:"solid 1px #eee", borderRight:"solid 1px #aaa", padding:"2px 4px"}
];

/**
 * 
 */
powerhua.cms._list_table = function(tObj, tData ) {

    // 验证
    if (tData == null || tData.length < 1) {
        return false;           
    }
    
    // 删除原来的Row
    tObj.removeChild(tObj.getElementsByTagName("TBODY")[0]);
    
    // 添加表内容
    var _tBody = document.createElement("TBODY");
    for(var i = 0; i < tData.length; i++) { 
    	var _tBodyRow = document.createElement("TR");
    	for(var j = 0; j < tData[i].length; j++) {
   			var _tBodyCell = document.createElement("TD")
   			var _tCellContent;
   			if(j == 0) {
   				_tCellContent = document.createElement("input");
   				_tCellContent.setAttribute("type", "checkbox");
   				_tCellContent.setAttribute("name", "check_content");
   				_tCellContent.setAttribute("value", tData[i][j]);
   				Event.observe(_tCellContent, "click", powerhua.cms._handleOnClick.bindAsEventListener(this, tObj, i, j), false);
   			} else if(j == 1) {
   				_tBodyCell.setAttribute("align", "center");
   				_tCellContent = document.createElement("img");
   				Element.setStyle(_tCellContent, {cursor: "pointer"});
   				_tCellContent.setAttribute("src", "../images/docs/star_off_unselected.gif");
   				//Event.observe(tdObjs[j], "click", powerhua.cms.gotoContentDetail.bindAsEventListener(this, tData[i]0j]), false);
   			} else {
   				_tCellContent = document.createTextNode(tData[i][j]);
   				Element.setStyle(_tBodyCell, {cursor: "pointer"});
   				Event.observe(_tBodyCell, "click", powerhua.cms.gotoContentDetail.bindAsEventListener(this, tData[i][0]), false); 
   			}
			_tBodyCell.appendChild(_tCellContent);
   			_tBodyRow.appendChild(_tBodyCell);
    	}
    	_tBody.appendChild(_tBodyRow);
    }
    tObj.appendChild(_tBody);  
}

/**
 *  
 */
powerhua.cms._list_table_style = function(tObj, styleData) {

	// 验证
    if (styleData == null || styleData.length < 1) {
        return false;           
    }
    
    // tr
	var trObjs = tObj.getElementsByTagName("TR");
	
	// body style
	for(var i = 0; i < trObjs.length; i++) {
		var tdObjs = trObjs[i].getElementsByTagName("TD");
		for(var j = 0; j < tdObjs.length; j++) {
			if(j < styleData.length) {
				Element.setStyle(tdObjs[j], styleData[j]);
			}
		}
	} 
}


powerhua.cms._actionEvent = function(tableObj) {
	var trObjs = tableObj.getElementsByTagName("TR");
	
	// head action
	var headTdObjs = trObjs[0].getElementsByTagName("TD");
		
	// body action
	var colNum = 0;
	for(var i = 0; i < trObjs.length; i++) {
		var tdObjs = trObjs[i].getElementsByTagName("TD");
		for(var j = 1; j < tdObjs.length; j++) {
			Element.setStyle(tdObjs[j], {cursor: "pointer"});
			
		}
	} 
}

Number.prototype.toHexFixed = function() {return isNaN(this) ? 'NaN' : (this < 16 ? '0' + this.toString(16) : this.toString(16))}

String.prototype.RGBtoHEX = function() {
		return /(\d+)\D+(\d+)\D+(\d+)/.test(this)? ['#', Number(RegExp.$1).toHexFixed().toUpperCase(), Number(RegExp.$2).toHexFixed().toUpperCase(), Number(RegExp.$3).toHexFixed().toUpperCase()].join ('') : NaN
}

	
/**
 *
 **/
powerhua.cms._handleOnClick = function(event, tableObj, rowNum, colNum) {
	powerhua.cms._setContentTableStyle(tableObj, rowNum, colNum, false, false);
}

/**
 *
 **/
powerhua.cms._setContentTableStyle = function(tableObj, rowNum, colNum, isAll, isGetAll) {
	var trObjs = tableObj.getElementsByTagName("TR");
	for(var i = 0; i < trObjs.length; i++) {
		if(i == rowNum) {
			var tdObjs = trObjs[i].getElementsByTagName("TD");
			for(var j = 0; j < tdObjs.length; j++) {
				var element = tdObjs[j];
				var color = Element.getStyle(element, "backgroundColor");
				var colorStr = "";
				
				if(Element.getStyle(element, "backgroundColor") != "NaN") {
					colorStr = color.RGBtoHEX();
				}
			
				if(!isAll) {
					if(colorStr != "#67A7E3") {
						Element.setStyle(element, {backgroundColor: "#67A7E3", color: "#FFFFFF"});
					} else {
						Element.setStyle(element, {backgroundColor: "#FFFFFF", color: "#000000"});
					}
				} else {
					if(isGetAll) {
						Element.setStyle(element, {backgroundColor: "#67A7E3", color: "#FFFFFF"});
					} else {
						Element.setStyle(element, {backgroundColor: "#FFFFFF", color: "#000000"});
					}
				}
			}
		}
	}
}


function selectAllContents() {
	var objs = document.getElementsByName("check_content");
	for(var i = 0; i < objs.length; i++) {
		objs[i].checked = true;
	}
	
	var tableObj = $("doc_table_list_content");
	var trObjs = tableObj.getElementsByTagName("TR");
	for(var i = 0; i < trObjs.length; i++) {
		var tdObjs = trObjs[i].getElementsByTagName("TD");
			powerhua.cms._setContentTableStyle(tableObj, i, 0, true, true);
	}
}

function unSelectAllContents() {
	var objs = document.getElementsByName("check_content");
	for(var i = 0; i < objs.length; i++) {
		objs[i].checked = false;
	}
	
	var tableObj = $("doc_table_list_content");
	var trObjs = tableObj.getElementsByTagName("TR");
	for(var i = 0; i < trObjs.length; i++) {
		var tdObjs = trObjs[i].getElementsByTagName("TD");
			powerhua.cms._setContentTableStyle(tableObj, i, 0, true, false);
	}
}

powerhua.cms.gotoContentDetail = function(event, contentId) {
	window.location.href = "./cms.do?s=contentAction&a=editContent&contentId=" + contentId;
}